package com.xiaode.validator;

import java.lang.reflect.Parameter;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.xiaode.plugin.SensitiveWord.SensitivewordFilter;

import cn.hutool.core.util.StrUtil;

public class SensitiveWordValidator implements Interceptor{

	@Override
	public void intercept(Invocation inv) {
		Parameter[] parameters = inv.getMethod().getParameters();// 获取方法的全部传参
		
        for (int index = 0; index < parameters.length; index++) {// 遍历全部参数
        	Object validObject = inv.getArg(index);// 获取传入参数的值
        	if(validObject != null && validObject instanceof String && StrUtil.isNotBlank((String) validObject)) {
        		Boolean b = SensitivewordFilter.isContaintSensitiveWord((String) validObject, 2);
        		if(b) {
        			Controller c = inv.getController();
        			Kv kv = Kv.by("msg", "包含敏感词汇！").set("code", 3001);
                    c.renderJson(Ret.fail().set("error", kv));
                	return;
        		}
        	}	
        }
        inv.invoke();
		
	}

}
